<?php

Class Controller 
{
	
	private $captcha;
	protected  $smarty;
	private $default_tpl='';
	function __construct()
	{
		
		//Model()->getCfg();
		
		
		
		$this->viewInit();
		if(method_exists($this,'_initialize'))
			$this->_initialize();
		
	} 
	/**
	 * 实例化smarty模板
	 * @access public
	 * @return void
	 * @author Hungry
	 */
	public function viewInit()
	{
		
		
		$this->smarty = new Smarty();
		if(GROUP_NAME!='Admin')
		{
		$this->default_tpl=C('home_tpl')?C('home_tpl').'/':DEAULT_TPL.'/';
		
// 		  if(MODULE_NAME!='user')
// 		  {
// 		  	$this->smarty->caching = true;
// 		  	$this->smarty->cache_dir = TEMP_PATH. '/Cache' ;
// 		  }
		}
		
		
	
		
		$template_dir=ROOT_PATH.GROUP_NAME.'/View/'.$this->default_tpl.ucfirst(MODULE_NAME);
		
		$this->smarty -> template_dir = $template_dir; //模板存放目录
		$this->smarty -> compile_dir = TEMP_PATH."/templates_c"; //编译目录
		$this->smarty -> left_delimiter = "{"; //左定界符
		$this->smarty -> right_delimiter = "}"; //右定界符
		
		
	}
	
	/**
	 * 输出信息
	 * @access public
	 * @return mixed
	 * @author Hungry
	 */
	public function showMsg($msg, $url='', $btn=array())
	{
		if(!is_array($msg))
		{
			$this->set_tpl_dir('Common');
			$this->assign('auto_redirect', $url);
			
			$this->assign('msg',$msg);
			$this-> display('message.html');
			exit;
		}
		else
		{
			print_r(json_encode($msg));
			exit();
		}
	}
	/**
	 * 显示验证码
	 * @access public
	 * @return void
	 * @author Hungry
	 */
    public function captcha()
    {
    	$this->captcha = new captchaController(ROOT_PATH . '/Temp/captcha/',100, 38);
    	@ob_end_clean(); //清除之前出现的多余输入
    	
    		$this->captcha->session_word = 'captcha_login';
    	
    	$this->captcha->generate_image();
    	
    }
    /**
     * 核对验证码
     * @access public
     * @return bool
     * @author Hungry
     */
    public function validator($post_captcha)
    {
    	    $this->captcha = new captchaController();
    	    $this->captcha->session_word = 'captcha_login';
            if (!$this->captcha->check_word($post_captcha))
            {
                return false;
            }
            else 
            {
            	return true;
            }
    }
  
    /**
     * 更换模板
     * @access private
     * @return void
     * @author Hungry
     */
    public function set_tpl_dir($moule_name='',$tpl='')
    {
    	$moule_name=$moule_name!=''?$moule_name:MODULE_NAME;
    	$tpl=$tpl!=''?$tpl.'/':$this->default_tpl;
    	$template_dir=ROOT_PATH.GROUP_NAME.'/View/'.$tpl.ucfirst($moule_name);
    	
		$this->smarty -> template_dir = $template_dir; 
    }
    public function is_cached($tpl, $cache_id='')
    {
    	return $this->smarty->isCached($tpl, $cache_id);
    }
    public function registerPlugin($type, $name, $callback, $acheable='',$cache_attrs='')
    {
    	return   $this->smarty->registerPlugin($type, $name, $callback, $acheable,$cache_attrs);
    } 
    public function fetch($tpl,$var)
    {
    
    	$this->smarty -> fetch($tpl,$var);
    
    }
    public function assign($tpl,$var)
    {
    
    	$this->smarty -> assign($tpl,$var);
    
    }
    
    public function display($tpl, $cache_id='')
    {

    	$this->smarty -> display($tpl, $cache_id);
    }
}